﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIM.model;

namespace LIM.controller
{
    public class DanhMucChiTieuController : BaseController
    {
        public DanhMucChiTieuController()
        { 
        }
        public List<tbl_DanhMuc_ChiTieu> Load()
        {
            return LIMS_Entities.tbl_DanhMuc_ChiTieu.ToList();
        }
        public tbl_DanhMuc_ChiTieu LoadByID(int RowIDChiTieu)
        {
            return LIMS_Entities.tbl_DanhMuc_ChiTieu.Where(p => p.RowID == RowIDChiTieu).FirstOrDefault();
        }
        public List<tbl_CRM_KhachHang> LoadKhachHang()
        {
            return LIMS_Entities.tbl_CRM_KhachHang.ToList();
        }
        public List<tbl_DanhMuc_ChiTieu_BangGia> LoadBangGiaByRowIDChiTieu(int RowIDChiTieu)
        {
            return LIMS_Entities.tbl_DanhMuc_ChiTieu_BangGia.Where(p => p.RowIDChiTieu == RowIDChiTieu).ToList();   
        }
        public bool SaveBangGia_ChiTieu(tbl_DanhMuc_ChiTieu_BangGia bgct)
        {
            bool Result = true;
            try
            {
                if (bgct.RowID == 0)
                {
                    LIMS_Entities.tbl_DanhMuc_ChiTieu_BangGia.AddObject(bgct);
                }
                LIMS_Entities.SaveChanges();
            }
            catch (Exception)
            {
                Result = false;
            }
            return Result;

        }
        public bool DeleteBangGia_ChiTieu(int RowIDChiTieu)
        {
            bool Result = true;
            try
            {
                foreach (tbl_DanhMuc_ChiTieu_BangGia item in LIMS_Entities.tbl_DanhMuc_ChiTieu_BangGia.Where(p => p.RowIDChiTieu == RowIDChiTieu).ToList())
                {
                    LIMS_Entities.tbl_DanhMuc_ChiTieu_BangGia.DeleteObject(item);
                    LIMS_Entities.SaveChanges();
                }
            }
            catch (Exception)
            {
                Result= false;
            }
            return Result;
                
        }
        public tbl_DanhMuc_ChiTieu_BangGia LoadGiaChiTieu(tbl_DanhMuc_ChiTieu tbl_DanhMuc_ChiTieu)
        {
            DateTime now = base.GetCurrentDateTimeFromDatabaseServer().Date;
            return tbl_DanhMuc_ChiTieu.tbl_DanhMuc_ChiTieu_BangGia.Where(c => c.NgayHieuLuc.Value.Date < now).OrderByDescending(o => o.NgayHieuLuc).FirstOrDefault();
        }
    }
}
